import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JOptionPane;

//hello1;0~andrew
public class ClientImpl extends UnicastRemoteObject implements Client{
    private Client client;
    private String clientName;


    protected ClientImpl() throws RemoteException {
    }

    public static void main(String[] args) throws Exception {
        Client client = new ClientImpl();
        //UnicastRemoteObject.exportObject(client);
        ServerSide stub = (ServerSide) Naming.lookup("rmi://192.168.3.13:1099/jackyluo");
        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
        String name = br1.readLine();
        stub.register(name,client);
        // 定义键盘输入流，用于读取用户键盘输入
        System.out.println("Please enter the message: ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line = br.readLine()) != null) {
            if(line.equals("logout")){
                stub.hello(client,null,line,null);
                break;
            }
            String[] lineArg = line.split(";");
            String[] receiver = lineArg[1].split("~");
            stub.hello(client, receiver[1], lineArg[0],receiver[0]);
            //stub.setMessage(line);
        }
    }
    // 实现允许远程调用的方法
    public void showDialog(String msg) throws java.rmi.RemoteException {
        // 使用JOptionPane显示对话框
        JOptionPane.showMessageDialog(null,msg);
//		System.out.println(this.client+"说"+msg);
    }

    public void showMessage(String content)throws java.rmi.RemoteException{
        System.out.println(content);
    }

    public void exit()throws java.rmi.RemoteException{
        System.exit(0);
    }

}
